﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Web;
using blackhouse.BlackApart.Web;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using blackhouse.mojoPortalExtend;
using System.Globalization;
using blackhouse.mojoPortalExtend.Breadcrumbs;
using blackhouse.WebNemo.Web.Controls;

namespace blackhouse.Aparts.Web.Pages {
	public partial class Events : NonCmsBasePage {

		#region Fields

		private IEnumerable<GroupRecord> events;
		private GroupRecord selectedEvent;
        private BreadcrumbsAccess breadcrumbs;

		#endregion

		#region Methods

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

            bool editUser = blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser().HasSufficientPermissions(false);
			this.events = new GroupRecords("wydarzenia_" + CultureInfo.CurrentCulture.Name.Split('-').First());
			if (!editUser)
				this.events = this.events.Where(ev => ev.GetBoolean("publiczna"));

            this.breadcrumbs = new BreadcrumbsAccess();
            this.breadcrumbs.AddCrumb("Wydarzenia w Łodzi", "/wydarzenia-miasto-lodz");

			// odczyt parametrów
			if (!string.IsNullOrEmpty(Request["eventId"])) {
				int eventId;
				if (Int32.TryParse(Request["eventId"], out  eventId)) {
					this.selectedEvent = this.events.FirstOrDefault(r => r.RecordId == eventId);
					if (this.selectedEvent != null) {
						this.events = this.events.Where(er => er.RecordId != this.selectedEvent.RecordId);
						this.bhSelectedEvent.PopulateEvent(this.selectedEvent);
						this.bhSelectedEvent.Visible = true;
						this.ltlDefaultTitle.Visible = false;
						this.ltlOnSelectedTitle.Visible = true;

                        this.breadcrumbs.AddCrumb(this.selectedEvent.GetString("tytul"), UrlUtils.LocalizeUrl(this.selectedEvent.GetString("url").ReplaceEx("~/", "/")));
					}
				}
			}

			if (this.selectedEvent == null) {
				PageProvider pp = new PageProvider();
				pp.SetCanonicalUrl("/wydarzenia-miasto-lodz");
				pp.SetPageTitle("Wydarzenia w Łodzi | Wybierasz się do Łodzi na jakieś wydarzenie? Zapraszamy do naszych apartmentów");
			}

			this.rptEvents.DataSource = this.events.OrderByDescending(r => r.CreatedTime);
			this.rptEvents.DataBind();

			if (editUser) {
                LinkToEdit edit = new LinkToEdit();
                edit.GroupSysName = "WYDARZENIA_PL";
                edit.OpenAsPopup = false;
                this.phActions.Controls.Add(edit);
			}

		}

		protected void rptEvents_ItemDataBound(object sender, RepeaterItemEventArgs e) {
			switch (e.Item.ItemType) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					GroupRecord record = e.Item.DataItem as GroupRecord;
					if (record == null) {
						e.Item.Visible = false;
						return;
					}

					HyperLink lnkImage = e.Item.FindControl("lnkImage") as HyperLink;
					HyperLink lnkLink = e.Item.FindControl("lnkLink") as HyperLink;
					HyperLink lnkLink2 = e.Item.FindControl("lnkLink2") as HyperLink;
					Literal ltlLead = e.Item.FindControl("ltlLead") as Literal;
					Literal ltlDate = e.Item.FindControl("ltlDate") as Literal;

					string url = record.GetString("url");
					url = UrlUtils.LocalizeUrl(url.ReplaceEx("~/", "/"));

					string title = record.GetString("tytul");
					string photoDesc = record.GetString("opis_zdjecia");

					if (lnkImage != null) {
						lnkImage.NavigateUrl = url;
						lnkImage.ToolTip = photoDesc;
						lnkImage.Text = photoDesc;
						lnkImage.ImageUrl = record.GetImageUrl("zdjecie", new System.Drawing.Size(250, 500));
					}

                    if (lnkLink != null) {
                        lnkLink.NavigateUrl = url;
                        lnkLink.ToolTip = title;
                        lnkLink.Text = title;
                    }
					if (lnkLink2 != null) {
						lnkLink2.NavigateUrl = url;
						lnkLink2.ToolTip = title;
					}

					if (ltlLead != null)
						ltlLead.Text = record.GetString("lid");

					if (ltlDate != null)
						ltlDate.Text = record.CreatedTime.ToString("g");

					break;
			}
		}

		#endregion

	}
}